package teach.aistar.controller;


import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import teach.aistar.model.entity.CarBrands;
import teach.aistar.model.entity.CarDetail;
import teach.aistar.model.entity.CarStyle;
import teach.aistar.model.vo.BuyCarsQueryVo;
import teach.aistar.model.vo.ProductQueryVo;
import teach.aistar.service.IBuyCarsService;
import teach.aistar.service.ICarBandsService;
import teach.aistar.service.ICarStyleService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Controller

public class BuyCarsController {

    @Autowired
    private IBuyCarsService iBuyCarsService;

    @Autowired
    private ICarBandsService iCarBandsService;

    @Autowired
    private ICarStyleService iCarStyleService;
    @GetMapping("/user/searchname")
    public String search(HttpServletRequest req)
    {

        String searchage = req.getParameter("searchname");
        System.out.println("searchname"+searchage);
        String pageNow = req.getParameter("pageNow");
        System.out.println(pageNow);
        if(null == pageNow || pageNow.trim().length()==0){
            pageNow = "1";
        }

       PageInfo<CarDetail> pageInfo = iBuyCarsService.findAge(Integer.valueOf(searchage),Integer.valueOf(pageNow),4);

        //取车品牌以及车的系列
        List<CarBrands> carBrands = iCarBandsService.selectAll();
        List<CarStyle> carStyles = iCarStyleService.findAll();

//        List<CarStyle> carStyles = iCarBandsService.selectStyle();
        req.setAttribute("carBrands",carBrands);
        req.setAttribute("carStyles",carStyles);

        req.setAttribute("pageInfo",pageInfo);
        req.setAttribute("pageInfo",pageInfo);

        return "/orange/buycar";

    }

    @GetMapping("/user/buycar")
    public String index(HttpServletRequest req){


        //获取pageNow
        String pageNow = req.getParameter("pageNow");
        System.out.println(pageNow);
        if(null == pageNow || pageNow.trim().length()==0){
            pageNow = "1";
        }

        String maxPrice = req.getParameter("maxPrice");
        String minPrice = req.getParameter("minPrice");
        String brands = req.getParameter("brands");
        String style = req.getParameter("style");
        String paiXu =req.getParameter("paiXu");

        System.out.println("价格不小雨"+minPrice);
        System.out.println("价格不超过"+maxPrice);
        System.out.println("品牌名"+brands);
        System.out.println("车型"+style);
        System.out.println("排序"+paiXu);
        ProductQueryVo vo = new ProductQueryVo();

        if(paiXu!=null)
        {
            vo.setPaiXu(Integer.valueOf(paiXu));
            req.setAttribute("paiXu",paiXu);
        }

        if(maxPrice!=null) {
            vo.setMaxPrice(Double.valueOf(maxPrice));
            req.setAttribute("maxPrice",maxPrice);
        }
        if(style!=null&&Integer.valueOf(style)>0)
        {
            vo.setStId(Integer.valueOf(style));
            req.setAttribute("style",style);
        }
        if(minPrice!=null)
        {
            vo.setMinPrice(Double.valueOf(minPrice));
            req.setAttribute("minPrice",minPrice);

        }
        if(brands!=null&&Integer.valueOf(brands)>0)
        {
            vo.setBrId(Integer.valueOf(brands));
            req.setAttribute("brands",brands);
        }


        PageInfo<CarDetail> pageInfo = iBuyCarsService.findAll(vo,Integer.valueOf(pageNow),4);
       pageInfo.getList().forEach(e-> System.out.println(e));


       //取车品牌以及车的系列
        List<CarBrands> carBrands = iCarBandsService.selectAll();
        List<CarStyle> carStyles = iCarStyleService.findAll();

//        List<CarStyle> carStyles = iCarBandsService.selectStyle();
        req.setAttribute("carBrands",carBrands);
        req.setAttribute("carStyles",carStyles);

        req.setAttribute("pageInfo",pageInfo);
//        model.addAttribute("carBrandsList",carBrandsList);
//        List<CarDetail> carDetailList = iCarDetailDao.findAll();
//        model.addAttribute("carDetailList",carDetailList);
        return "/orange/buycar";


    }
//    @GetMapping("/selects")
//    public String selects(HttpServletRequest request, HttpSession session) throws ParseException {
////        String brands = request.getParameter("brands");
////        String registration = request.getParameter("registration");
////        String minPrice = request.getParameter("minPrice");
//        String maxPrice = request.getParameter("maxPrice");
////        String gear = request.getParameter("gear");
//
//        BuyCarsQueryVo buyCarsQueryVo = new BuyCarsQueryVo();
//
////        String searchName = request.getParameter("searchName");
////        BuyCarsQueryVo queryVo = new BuyCarsQueryVo();
////        queryVo.setName(searchName);
////
////        if (minPrice != null && minPrice.trim().length() > 0) {
////            buyCarsQueryVo.setMinPrice(Double.valueOf(minPrice));
////        }
//
//        if (maxPrice != null && maxPrice.trim().length() > 0) {
//            buyCarsQueryVo.setMaxPrice(Double.valueOf(maxPrice));
//        }
//
////        if (brands.equals("-1")) {
////            brands = null;
////        }
////        if (brands != null && brands.trim().length() > 0) {
////            buyCarsQueryVo.setBrId(Long.valueOf(brands));
////        }
//
////        List<CarDetail> byConditional = iBuyCarsService.findAll(buyCarsQueryVo);
//
//        request.setAttribute("brands", brands);
//        request.setAttribute("registration", registration);
//        request.setAttribute("minPrice", minPrice);
//        request.setAttribute("maxPrice", maxPrice);
//        request.setAttribute("gear", gear);
//
//
//        return "/orange/buycar";
//    }
}
